package fr.red5.components
{
	import mx.core.UIComponent;
	import mx.controls.TextInput;
	import mx.controls.Button;
	import mx.containers.HBox;
	import mx.controls.Text;
	import flash.events.*;
	
	public class SimpleConnect extends UIComponent
	{
		
		import mx.core.UIComponent;
		import mx.controls.Alert;
		import flash.net.*;
		import fr.red5.components.IComponent;
		import fr.red5.net.Red5Connection;
		import fr.red5.managers.UserManager;
		import fr.red5.events.*;
		
		private var usernameTextInput:TextInput;
		private var submitButton:Button;
		
		
		XML.ignoreComments = false;
		XML.ignoreProcessingInstructions = false;
		
		private var config:XML;
		private var loader:URLLoader;
		private var CONFIG_URL:String = "red5-config.xml";
		
		
		[Bindable] private var username:String= "Visiteur";
		[Bindable] private var busy:Boolean = false;
		
		private var SERVER_URL:String;
		private var r5:Red5Connection;		
	
		public function SimpleConnect():void
		{
			initConfig();
			initUI();
		}
		
		
		private function initConfig():void
		{
			config = new XML();
			var configRequest:URLRequest = new URLRequest(CONFIG_URL);
			loader = new URLLoader(configRequest);
			loader.addEventListener("complete", configLoaded);
			var so:SharedObject= SharedObject.getLocal("red5.fr");
 			if(so.data.username != undefined){
 				username = so.data.username;
 			}
 			busy = true			
		}
		
		private function initUI():void 
		{	
			var hbox:HBox = new HBox();
			hbox.width= 1;
			var txt:Text = new Text();
			txt.text = "NickName";
			usernameTextInput = new TextInput();
			usernameTextInput.maxChars = 30;
			usernameTextInput.text = username;
			usernameTextInput.enabled = !busy;
			submitButton = new Button();
			submitButton.label = "Modifier";
			submitButton.addEventListener(MouseEvent.CLICK,updateUsername);
			submitButton.enabled = ((usernameTextInput.text.length > 0) || !busy)
			hbox.addChild(txt);
			hbox.addChild(usernameTextInput);
			hbox.addChild(submitButton);
			addChild(hbox);	
			
		}
		private function configLoaded(evtObj:Event):void
		{
		    config = XML(loader.data);
		    SERVER_URL = config.prop.(@id=='protocol')+"://"+ config.prop.(@id=='server')+"/"+config.prop.(@id=='app');
		    connect();
		}
		
		private function connect():void{
			r5 = new Red5Connection();
			r5.server = SERVER_URL;
			r5.username = username
			r5.addEventListener(Red5ErrorEvent.ERROR,onError);
			r5.addEventListener(ConnectionSuccessEvent.CONNECT_SUCCESS, onSuccess);		
		}		
		
		protected function onError(event:Red5ErrorEvent):void
		{
			Alert.show("Erreur: "+event.errorMessage);
		}
		
				
		private function onSuccess(event:Event):void {
			busy = false
		}
			
									
		private function updateUsername():void{
			if(username != usernameTextInput.text){
				username = usernameTextInput.text
				var so:SharedObject= SharedObject.getLocal("red5.fr");
				so.data.username = username;
				so.flush();
			}
		}
				
	}
}